package com.laozhao.msas.view;

import java.awt.Image;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.event.IIOReadProgressListener;
import javax.imageio.stream.ImageInputStream;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
import javax.swing.event.ListSelectionEvent;

public class ImageRetriever extends SwingWorker<Icon, Void> {
	private ImageRetriever() {
	}

	public ImageRetriever(JLabel lblImage, String strImageUrl) {
		this.strImageUrl = strImageUrl;
		this.lblImage = lblImage;
	}

	@Override
	protected Icon doInBackground() throws Exception {
		Icon icon = retrieveImage(strImageUrl);
		return icon;
	}

	private Icon retrieveImage(String strImageUrl)
			throws MalformedURLException, IOException {
		InputStream is = null;
		URL imgUrl = null;
		imgUrl = new URL(strImageUrl);
		is = imgUrl.openStream();
		ImageInputStream iis = ImageIO.createImageInputStream(is);
		Iterator<ImageReader> it = ImageIO.getImageReadersBySuffix("jpg");
		ImageReader reader = it.next();
		reader.setInput(iis);
		reader.addIIOReadProgressListener(new IIOReadProgressListener() {
			public void imageProgress(ImageReader source, float percentageDone) {
				setProgress((int) percentageDone);
			}

			public void imageComplete(ImageReader source) {
				setProgress(100);
			}

			@Override
			public void imageStarted(ImageReader source, int imageIndex) {
				// TODO Auto-generated method stub

			}

			@Override
			public void readAborted(ImageReader source) {
				// TODO Auto-generated method stub

			}

			@Override
			public void sequenceComplete(ImageReader source) {
				// TODO Auto-generated method stub

			}

			@Override
			public void sequenceStarted(ImageReader source, int minIndex) {
				// TODO Auto-generated method stub

			}

			@Override
			public void thumbnailComplete(ImageReader source) {
				// TODO Auto-generated method stub

			}

			@Override
			public void thumbnailProgress(ImageReader source,
					float percentageDone) {
				// TODO Auto-generated method stub

			}

			@Override
			public void thumbnailStarted(ImageReader source, int imageIndex,
					int thumbnailIndex) {
				// TODO Auto-generated method stub

			}
		});
		Image image = reader.read(0);
		Icon icon = new ImageIcon(image);
		return icon;
	}

	@Override
	protected void done() {
		Icon icon = null;
		String text = null;
		try {
			icon = get();
		} catch (Exception ignore) {
			ignore.printStackTrace();
			text = "Image unavailable";
		}
		lblImage.setIcon(icon);
		lblImage.setText(text);
	}

	private String strImageUrl;
	private JLabel lblImage;

	private void listImagesValueChanged(ListSelectionEvent evt) {
//		ImageInfo info = (ImageInfo) listImages.getSelectedValue();
//		String id = info.getId();
//		String server = info.getServer();
//		String secret = info.getSecret(); // No need to search an invalid thumbnail image 
//		if (id == null || server == null || secret == null) {
//			return;
//		}
//		String strImageUrl = String.format(IMAGE_URL_FORMAT, server, id, secret);
//		retrieveImage(strImageUrl); 
		//... 
	}
	
//	private void retrieveImage(String imageUrl) {
		// SwingWorker objects can't
		// be reused, so // create a
		// new one as needed.
		// ImageRetriever
		// imgRetriever = new
		// ImageRetriever(lblImage,
		// imageUrl);
		// progressSelectedImage.setValue(0);
		// // Listen for changes in
		// the "progress" property.
		// // You can reuse the
		// listener even though the
		// worker thread // will be
		// a new SwingWorker.
		// imgRetriever.addPropertyChangeListener(listenerSelectedImage);
		// progressSelectedImage.setIndeterminate(true);
		// // Tell the worker thread
		// to begin with this
		// asynchronous method.
		// imgRetriever.execute();
		// // This event thread
		// continues immediately
		// here without blocking. 
//		}
//	}
	
	/**
	 * ProgressListener listens to "progress" property * changes in the
	 * SwingWorkers that search and load * images.
	 */
	class ProgressListener implements PropertyChangeListener {
		// Prevent creation without providing a progress bar.
		private ProgressListener() {
		}

		ProgressListener(JProgressBar progressBar) {
			this.progressBar = progressBar;
			this.progressBar.setValue(0);
		}

		public void propertyChange(PropertyChangeEvent evt) {
			String strPropertyName = evt.getPropertyName();
			if ("progress".equals(strPropertyName)) {
				progressBar.setIndeterminate(false);
				int progress = (Integer) evt.getNewValue();
				progressBar.setValue(progress);
			}
		}

		private JProgressBar progressBar;

	}
}
